package io.gitee.chearnee.fw.web.config;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.StringUtils;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2023/4/11 16:40
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnClass(EndpointAutoConfiguration.class)
public class WebEndpointConfiguration {

    private final WebEndpointProperties webEndpointProperties;

    public boolean isEndpointUri(ServerHttpRequest serverHttpRequest) {
        if (webEndpointProperties != null && StringUtils.hasText(webEndpointProperties.getBasePath()) && (serverHttpRequest instanceof ServletServerHttpRequest slt)) {
            String requestURI = slt.getServletRequest().getRequestURI();
            return requestURI.startsWith(webEndpointProperties.getBasePath());
        }
        return false;
    }
}
